using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Globalization;
using System.Drawing;
using System.Data.Odbc;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.Windows.Media;

using Newtonsoft.Json.Linq;

using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.WoWInternals;

namespace WatchMyToon
{
    public class Functions_Chat
    {
        public SQL_Connector SQLConnector = new SQL_Connector();
        // Send something to the Chat
        public void SendToWoWChat(string MessageType, string szMessage, string szReciever)
        {

            switch (MessageType)
            {
                case "WHISPER_MESSAGE":
                    Logging.Write(LogLevel.Normal, Colors.Blue, "[WatchMyToon] - Send WHISPER_MESSAGE");
                    Lua.DoString("SendChatMessage(\"" + szMessage + "\", \"WHISPER\", nil, \"" + szReciever + "\");");
                    break;
                case "SAY_MESSAGE":
                    Logging.Write(LogLevel.Normal, Colors.Blue, "[WatchMyToon] - Send SAY_MESSAGE");
                    Lua.DoString("SendChatMessage(\"" + szMessage + "\", \"SAY\", nil, nil);");
                    break;
                case "GUILD_MESSAGE":
                    Logging.Write(LogLevel.Normal, Colors.Blue, "[WatchMyToon] - Send GUILD_MESSAGE");
                    Lua.DoString("SendChatMessage(\"" + szMessage + "\", \"GUILD\", nil, nil);");
                    break;
                case "YELL_MESSAGE":
                    Logging.Write(LogLevel.Normal, Colors.Blue, "[WatchMyToon] - Send YELL_MESSAGE");
                    Lua.DoString("SendChatMessage(\"" + szMessage + "\", \"YELL\", nil, nil);");
                    break;
                case "BATTLEGROUND_MESSAGE":
                    Logging.Write(LogLevel.Normal, Colors.Blue, "[WatchMyToon] - Send BATTLEGROUND_MESSAGE");
                    Lua.DoString("SendChatMessage(\"" + szMessage + "\", \"BATTLEGROUND\", nil, nil);");
                    break;
                case "PARTY_MESSAGE":
                    Logging.Write(LogLevel.Normal, Colors.Blue, "[WatchMyToon] - Send PARTY_MESSAGE");
                    Lua.DoString("SendChatMessage(\"" + szMessage + "\", \"PARTY\", nil, nil);");
                    break;
                case "RAID_MESSAGE":
                    Logging.Write(LogLevel.Normal, Colors.Blue, "[WatchMyToon] - Send RAID_MESSAGE");
                    Lua.DoString("SendChatMessage(\"" + szMessage + "\", \"RAID\", nil, nil);");
                    break;
                default:
                    break;

            }
        }

        //BattleGround Event
        public void WoWChat_Battleground(Styx.CommonBot.Chat.ChatLanguageSpecificEventArgs e)
        {
            if (e.Author != StyxWoW.Me.Name)
            {
                try
                {
                    string sMessageType = "BATTLEGROUND_MESSAGE";
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABMessageReceive\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szMessageType\":\"" + sMessageType + "\"},{\"szMessagePartner\":\"" + e.Author + "\"},{\"szMessageText\":\"" + e.Message + "\"}]", "{\"Query\":\"Insert\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - BattleGround Message Insert Report: " + sCommandCheck);

                    Logging.Write(LogLevel.Normal, Colors.LightBlue, "[WatchMyToon] - Received " + sMessageType);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - WoWChat_Battleground SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
        }

        //Whisper event
        public void WoWChat_Whisper(Styx.CommonBot.Chat.ChatWhisperEventArgs e)
        {
            if (e.Author != StyxWoW.Me.Name)
            {
                try
                {
                    string sMessageType = "WHISPER_MESSAGE";
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABMessageReceive\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szMessageType\":\"" + sMessageType + "\"},{\"szMessagePartner\":\"" + e.Author + "\"},{\"szMessageText\":\"" + e.Message + "\"}]", "{\"Query\":\"Insert\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Whisper Message Insert Report: " + sCommandCheck);

                    Logging.Write(LogLevel.Normal, Colors.LightBlue, "[WatchMyToon] - Received " + sMessageType);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - WoWChat_Whisper SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
        }

        //Party Message
        public void WoWChat_Party(Styx.CommonBot.Chat.ChatLanguageSpecificEventArgs e)
        {
            if (e.Author != StyxWoW.Me.Name)
            {
                try
                {
                    string sMessageType = "PARTY_MESSAGE";
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABMessageReceive\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szMessageType\":\"" + sMessageType + "\"},{\"szMessagePartner\":\"" + e.Author + "\"},{\"szMessageText\":\"" + e.Message + "\"}]", "{\"Query\":\"Insert\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Party Message Insert Report: " + sCommandCheck);

                    Logging.Write(LogLevel.Normal, Colors.LightBlue, "[WatchMyToon] - Received " + sMessageType);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - WoWChat_Party SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
        }

        //Guild Event
        public void WoWChat_Guild(Styx.CommonBot.Chat.ChatGuildEventArgs e)
        {
            if (e.Author != StyxWoW.Me.Name)
            {
                try
                {
                    string sMessageType = "GUILD_MESSAGE";
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABMessageReceive\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szMessageType\":\"" + sMessageType + "\"},{\"szMessagePartner\":\"" + e.Author + "\"},{\"szMessageText\":\"" + e.Message + "\"}]", "{\"Query\":\"Insert\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Guild Message Insert Report: " + sCommandCheck);

                    Logging.Write(LogLevel.Normal, Colors.LightBlue, "[WatchMyToon] - Received " + sMessageType);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - WoWChat_Guild SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
        }

        //Say Event
        public void WoWChat_Say(Styx.CommonBot.Chat.ChatLanguageSpecificEventArgs e)
        {
            if (e.Author != StyxWoW.Me.Name)
            {
                try
                {
                    string sMessageType = "SAY_MESSAGE";
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABMessageReceive\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szMessageType\":\"" + sMessageType + "\"},{\"szMessagePartner\":\"" + e.Author + "\"},{\"szMessageText\":\"" + e.Message + "\"}]", "{\"Query\":\"Insert\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Say Message Insert Report: " + sCommandCheck);

                    Logging.Write(LogLevel.Normal, Colors.LightBlue, "[WatchMyToon] - Received " + sMessageType);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - WoWChat_Say SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
        }

        //Yell Event
        public void WoWChat_Yell(Styx.CommonBot.Chat.ChatLanguageSpecificEventArgs e)
        {
            if (e.Author != StyxWoW.Me.Name)
            {
                try
                {
                    string sMessageType = "YELL_MESSAGE";
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABMessageReceive\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szMessageType\":\"" + sMessageType + "\"},{\"szMessagePartner\":\"" + e.Author + "\"},{\"szMessageText\":\"" + e.Message + "\"}]", "{\"Query\":\"Insert\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Yell Message Insert Report: " + sCommandCheck);

                    Logging.Write(LogLevel.Normal, Colors.LightBlue, "[WatchMyToon] - Received " + sMessageType);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - WoWChat_Yell SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
        }

        //Raid Event
        public void WoWChat_Raid(Styx.CommonBot.Chat.ChatLanguageSpecificEventArgs e)
        {
            if (e.Author != StyxWoW.Me.Name)
            {
                try
                {
                    string sMessageType = "RAID_MESSAGE";
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABMessageReceive\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szMessageType\":\"" + sMessageType + "\"},{\"szMessagePartner\":\"" + e.Author + "\"},{\"szMessageText\":\"" + e.Message + "\"}]", "{\"Query\":\"Insert\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Raid Message Insert Report: " + sCommandCheck);

                    Logging.Write(LogLevel.Normal, Colors.LightBlue, "[WatchMyToon] - Received " + sMessageType);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - WoWChat_Raid SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
        }

        //Battle.Net Whisper Event
        public void WoWChat_BattleNet_Whisper(object sender, LuaEventArgs args)
         {
             /*
                 arg1
                     chat message 
                 arg2
                     author 
                 arg6
                     AFK/DND/GM "CHAT_FLAG_"..arg6 flags 
                 arg11
                     Chat lineID 
                 arg13
                     Bnet presenceID 
             */
             if (args.Args[2] != StyxWoW.Me.Name)
             {
                 try
                 {
                     string sMessageType = "BNET_MESSAGE";
                     string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABMessageReceive\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szMessageType\":\"" + sMessageType + "\"},{\"szMessagePartner\":\"" + args.Args[2] + "\"},{\"szMessageText\":\"" + args.Args[1] + "\"}]", "{\"Query\":\"Insert\"}", "{\"Limit\":\"0\"}");
                     Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - GM Message Insert Report: " + sCommandCheck);

                     Logging.Write(LogLevel.Normal, Colors.LightBlue, "[WatchMyToon] - Received " + sMessageType);

                 }
                 catch (Exception ex)
                 {
                     Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - WoWChat_BattleNet_Whisper SQL Failed!");
                     Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                 }
             }
        }
    }
}
